﻿/**
 * 
 */
package com.ssyt.ssh.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssyt.ssh.dao.EmpDAO;
import com.ssyt.ssh.entity.Department;
import com.ssyt.ssh.entity.Employee;
import com.ssyt.ssh.entity.PageBean;
import com.ssyt.ssh.exception.MyBusinessException;

/**
 * @author Administrator
 *
 */
@Service("empService")
public class EmpServiceImpl implements EmpService {

	@Autowired
	private EmpDAO empDao;
	/* (non-Javadoc)
	 * @see com.ssyt.ssh.service.EmpService#addEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public void addEmp(Employee emp) {
		try {
			empDao.addEmp(emp);
		} catch (Exception e) {
			throw new MyBusinessException("员工新增失败");
		}

	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.service.EmpService#deleteEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public void deleteEmp(Employee emp) {
		try {
			empDao.deleteEmp(emp);
		} catch (Throwable e) {
			System.out.println("==============");
			e.printStackTrace();
			throw new MyBusinessException("员工删除失败");
		}
	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.service.EmpService#getEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public Employee getEmp(Employee emp) {
		return empDao.getEmp(emp);
	}
/*<!-- class 所对应的是empaction 注解 @Controller("empAction") 属于spring管理范围 -->*/
	/* (non-Javadoc)
	 * @see com.ssyt.ssh.service.EmpService#updateEmp(com.ssyt.ssh.entity.Employee)
	 */
	@Override
	public void updateEmp(Employee emp) {
		try {
			empDao.updateEmp(emp);
		} catch (Exception e) {
			throw new MyBusinessException("员工修改失败");
		}

	}

	/* (non-Javadoc)
	 * @see com.ssyt.ssh.service.EmpService#listEmpPagnation(com.ssyt.ssh.entity.Employee, com.ssyt.ssh.entity.PageBean)
	 */
	@Override
	public PageBean listEmpPagnation(Employee emp, PageBean pageBean) {
		// 获取总记录数
		int totalRows = empDao.countEmp(emp);
		// 获取当前页
		int currentPage = pageBean.getCurrentPage();
		// 获取每页显示数
		int pageSize = pageBean.getPageSize();
		// 获取每页显示记录
		List<?> list = empDao.listEmp(emp, pageSize, currentPage);
		// 创建一个分页对象
		PageBean pager = new PageBean(totalRows, currentPage, pageSize);
		// 设置每页显示集合
		pager.setList(list);
		
		return pager;
	}

	@Override
	public List<Department> getDepts() {
		return empDao.getDepts();
	}

}
